import App from "./App"
// #ifndef VUE3
import Vue from "vue"
// import request from '@/uni/request'

import { backend } from "uni/index"
import store from "./store/index"
import uView from "uview-ui"
Vue.use(uView)
// 公共组件集合开始
import tips from "@/components/tips/index.js"
Vue.use(tips)
import cButton from "@/components/my-button/my-button.vue"
Vue.component("cButton", cButton)
import cSearch from "@/components/my-search/my-search.vue"
Vue.component("cSearch", cSearch)
import cIcon from "@/components/arrow-icon/arrow-icon.vue"
Vue.component("cIcon", cIcon)
import tab from "@/components/tab/tab.vue"
Vue.component("tab", tab)
import phonePicker from "@/components/phone-picker/phone-picker.vue"
Vue.component("phonePicker", phonePicker)
import upgrade from "@/components/version-upgrade/version-upgrade.vue"
Vue.component("upgrade", upgrade)
import numPalaceOut from "@/components/num-palace/out.vue"
Vue.component("numPalaceOut", numPalaceOut)
import numPalaceIn from "@/components/num-palace/in.vue"
Vue.component("numPalaceIn", numPalaceIn)
import uploadImg from "@/components/upload-img/upload-img.vue"
Vue.component("uploadImg", uploadImg)
import timeLine from "@/components/time-line/time-line.vue"
Vue.component("timeLine", timeLine)
// 公共组件集合结束
Vue.prototype.$message = {
	success(title) {
		uni.showToast({ title })
	},
	error(title) {
		uni.showToast({
			title,
			icon: "none",
		})
	},
	loading(flag = true, title) {
		flag ? uni.showLoading({ title, mask: true }) : uni.hideLoading()
	},
	modal(option = { title: "", content: "", success: () => {}, error: () => {} }) {
		const { title, content, success, error } = option
		uni.showModal({
			title,
			content,
			success: ({ confirm, cancel }) => {
				if (confirm) {
					success && success()
				} else if (cancel) {
					error && error()
				}
			},
		})
	},
	action(
		option = {
			list: [],
			key: "",
			success: () => {},
			error: () => {},
		},
	) {
		const { list, key, success, error } = option
		uni.showActionSheet({
			itemList: list.map(item => (key ? item[key] : item)),
			success: ({ tapIndex }) => {
				success && success(tapIndex)
				// success && success(list[tapIndex]);
			},
			fail: ({ errMsg }) => {
				error && error(errMsg)
			},
		})
	},
}
Vue.prototype.uploadFile = (cb, sourceType = ["album", "camera"]) => {
	uni.chooseImage({
		count: 1, //默认9
		sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
		sourceType: sourceType,
		success: function (res) {
			const { tempFiles, tempFilePaths } = res
			uni.uploadFile({
				url: backend + "/api/oss/upload", //仅为示例，并非真实接口地址。
				file: tempFiles[0],
				filePath: tempFilePaths[0],
				name: "upfile",
				header: {
					Authorization: uni.getStorageSync("token") ? "Bearer " + uni.getStorageSync("token") : "",
				},
				success: resoult => {
					if (Array.isArray(resoult)) {
						cb && cb(JSON.parse(resoult[1].data).data.url)
					} else {
						cb && cb(JSON.parse(resoult.data).data.url)
					}
				},
				fail(e) {
					console.log(e)
				},
			})
		},
	})
}
Vue.prototype.$store = store

Vue.config.productionTip = false
App.mpType = "app"
const app = new Vue({
	...App,
	store,
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from "vue"
export function createApp() {
	const app = createSSRApp(App)
	return {
		app,
		store,
	}
}
// #endif
